﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web

Public Class SaleModifyHFSP
    Inherits BusinessDomainBase

    Private _businessGUID As String     '销售变更申请GUID
    Private _saleModiSP As SaleModiSP   '销售变更审批
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/slxt/SaleModifyHFSP.xml")
        Me._xml.LoadXml(MyBase.XML)
        Me._saleModiSP = New SaleModiSP()
    End Sub

    '属性：销售变更申请GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return Me._businessGUID
        End Get
        Set(ByVal Value As String)
            Me._businessGUID = Value
            Me._saleModiSP.BusinessGUID = Me._businessGUID
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return Me._xml.OuterXml
        End Get
        Set(ByVal Value As String)
            Me._xml.LoadXml(Value)
        End Set
    End Property

    '功能：取销售变更申请数据，填充XML
    Public Overrides Function LoadData() As Integer

        Dim strSQL As String
        Dim i, j, iRowCount As Integer
        Dim dtTemp As DataTable

        '主记录    
        strSQL = "SELECT SaleModiApplyGUID, SaleGUID, SaleType, ApplyType, ApplyState, ApproveState, BUGUID, ProjGUID, " & _
                 " RoomGUID, RoomInfo, NowRoomGUID, NowRoomInfo, CstGUID, CstName, ReasonSort, Reason, ApplyBy, CONVERT(varchar(10), ApplyDate, 120) as ApplyDate, HandCharge, Pay " & _
                 " FROM vs_SaleModiApply_WF " & _
                 " WHERE SaleModiApplyGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then

            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID

            '主记录
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售变更申请GUID']").InnerText = dtTemp.Rows(0).Item("SaleModiApplyGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单GUID']").InnerText = dtTemp.Rows(0).Item("SaleGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='销售单类型']").InnerText = dtTemp.Rows(0).Item("SaleType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请类型']").InnerText = dtTemp.Rows(0).Item("ApplyType").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请单状态']").InnerText = dtTemp.Rows(0).Item("ApplyState").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='审批状态']").InnerText = dtTemp.Rows(0).Item("ApproveState").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='单位GUID']").InnerText = dtTemp.Rows(0).Item("BUGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='项目GUID']").InnerText = dtTemp.Rows(0).Item("ProjGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户GUID']").InnerText = dtTemp.Rows(0).Item("CstGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户名称']").InnerText = dtTemp.Rows(0).Item("CstName").ToString

            Dim cstGUIDList As String() = dtTemp.Rows(0).Item("CstGUID").ToString().Split(";")
            Dim cstNameList As String() = dtTemp.Rows(0).Item("CstName").ToString().Split(";")

            If cstGUIDList.Count < 5 Then

                For i = 0 To cstGUIDList.Count - 1

                    j = i + 1
                    _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户GUID" & j.ToString() & "']").InnerText = cstGUIDList(i)
                    _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='客户名称" & j.ToString() & "']").InnerText = cstNameList(i)

                Next

            End If

            '变更前房间信息
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间GUID']").InnerText = dtTemp.Rows(0).Item("RoomGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间信息']").InnerText = dtTemp.Rows(0).Item("RoomInfo").ToString

            '变更信息
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因分类']").InnerText = dtTemp.Rows(0).Item("ReasonSort").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='原因说明']").InnerText = dtTemp.Rows(0).Item("Reason").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请人']").InnerText = dtTemp.Rows(0).Item("ApplyBy").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请日期']").InnerText = dtTemp.Rows(0).Item("ApplyDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='手续费']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("HandCharge")).ToString("0.00")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='已交金额']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(0).Item("Pay")).ToString("0.00")

            '变更后房间信息
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间GUID_变更后']").InnerText = dtTemp.Rows(0).Item("NowRoomGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='房间信息_变更后']").InnerText = dtTemp.Rows(0).Item("NowRoomInfo").ToString


            If dtTemp.Rows(0).Item("SaleModiApplyGUID").ToString() <> "" AndAlso dtTemp.Rows(0).Item("SaleGUID").ToString() <> "" Then

                strSQL = "SELECT 1 as entity,(case when b.mainroomguid is null then 0 else 1 end) as IsBind,a.OCAttachRoomGUID,b.RoomInfo,a.RoomGUID,isnull(a.total,0) as total,isnull(a.DiscntValue,100) as DiscntValue,isnull(a.CjTotal,0) as CjTotal,isnull(a.BldArea,0) as BldArea,isnull(a.TnArea,0) as TnArea,isnull(a.Price,0) as Price,isnull(a.TnPrice,0) as TnPrice,case when s_SModiAttachRoom.RoomGUID is null then '否' else '是' end as isChecked  FROM s_OCAttachRoom a INNER JOIN ep_Room b  ON a.RoomGUID=b.RoomGUID left join s_SModiAttachRoom  ON a.RoomGUID=s_SModiAttachRoom.RoomGUID and SaleModiApplyGUID=(case '" & dtTemp.Rows(0).Item("SaleModiApplyGUID").ToString() & "' when '' then null else '" & dtTemp.Rows(0).Item("SaleModiApplyGUID").ToString() & "' end) WHERE SaleGUID=(case '" & dtTemp.Rows(0).Item("SaleGUID").ToString() & "' when '' then null else '" & dtTemp.Rows(0).Item("SaleGUID").ToString() & "' end)"

                dtTemp = MyDB.GetDataTable(strSQL)

                iRowCount = dtTemp.Rows.Count - 1

                If iRowCount >= 0 Then

                    Dim BZZJTotal As Decimal = 0
                    Dim XYZJTotal As Decimal = 0
                    Dim group As XmlNode = _xml.SelectSingleNode("/BusinessType/Item/Group")
                    Dim item As XmlNode = group.SelectSingleNode("Item")
                    Dim items As XmlNodeList = group.SelectNodes("Item")

                    For Each node As XmlNode In items
                        group.RemoveChild(node)
                    Next

                    For i = 0 To iRowCount

                        Dim newXmlNode As XmlElement = item.Clone()

                        newXmlNode.SelectSingleNode("Domain[@name='序号']").InnerText = i + 1

                        If Not newXmlNode.SelectSingleNode("Domain[@name='附属GUID']") Is Nothing Then
                            newXmlNode.SelectSingleNode("Domain[@name='附属GUID']").InnerText = dtTemp.Rows(i).Item("OCAttachRoomGUID").ToString()
                        End If

                        If Not newXmlNode.SelectSingleNode("Domain[@name='附属_房间GUID']") Is Nothing Then
                            newXmlNode.SelectSingleNode("Domain[@name='附属_房间GUID']").InnerText = dtTemp.Rows(i).Item("RoomGUID").ToString()
                        End If

                        newXmlNode.SelectSingleNode("Domain[@name='附属_房间信息']").InnerText = dtTemp.Rows(i).Item("RoomInfo").ToString()
                        newXmlNode.SelectSingleNode("Domain[@name='附属_标准总价']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(i).Item("Total").ToString()).ToString("0.00")
                        newXmlNode.SelectSingleNode("Domain[@name='附属_折扣']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(i).Item("DiscntValue").ToString()).ToString("0.00")
                        newXmlNode.SelectSingleNode("Domain[@name='附属_协议总价_人民币']").InnerText = System.Convert.ToDecimal("0" + dtTemp.Rows(i).Item("CjTotal").ToString()).ToString("0.00")
                        newXmlNode.SelectSingleNode("Domain[@name='附属_是否换入到新房间']").InnerText = dtTemp.Rows(i).Item("isChecked").ToString()

                        BZZJTotal += Decimal.Parse("0" + dtTemp.Rows(i).Item("Total").ToString())
                        XYZJTotal += Decimal.Parse("0" + dtTemp.Rows(i).Item("CjTotal").ToString())

                        group.AppendChild(newXmlNode)

                    Next

                    _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='附属_标准总价_合计']").InnerText = System.Convert.ToDecimal("0" + BZZJTotal.ToString()).ToString("0.00")
                    _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='附属_协议总价_人民币_合计']").InnerText = System.Convert.ToDecimal("0" + XYZJTotal.ToString()).ToString("0.00")
                End If

            End If

        End If

        Return 0

    End Function

    '功能：发起“销售变更”的审批流程
    Public Overrides Function Initiate() As String

        Return Me._saleModiSP.Initiate()

    End Function

    '功能：根据XML进行数据对“销售变更”进行归档
    Public Overrides Function SaveData() As String

        Return Me._saleModiSP.SaveData()

    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String

        Return Me._saleModiSP.SaveData_NoPass()

    End Function

    '功能：审批作废处理
    Public Overrides Function SaveData_Cancel() As String

        Return Me._saleModiSP.SaveData_Cancel()

    End Function

    '功能：发起撤回“销售变更”的审批流程
    Public Overrides Function InitiateWithDrawn() As String

        '由于发起撤回的审批流程和审批作废处理是一样的，所以直接调用审批作废的方法即可
        Return Me._saleModiSP.InitiateWithDrawn()

    End Function

End Class
